package com.asiainfo.heshanghui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.asiainfo.heshanghui.adapter_recyclerview.HorizontalDividerItemDecoration2;
import com.asiainfo.heshanghui.adapter_recyclerview.UniversalAdapter;
import com.asiainfo.heshanghui.configs.ConstantsME;
import com.asiainfo.heshanghui.configs.EventBusTag;
import com.asiainfo.heshanghui.configs.InterfaceConfig;
import com.asiainfo.heshanghui.entity.GoodsTypeInfo;
import com.asiainfo.heshanghui.networkModel.HttpUtil;
import com.framework.Utils.PreferencesHelper;
import com.framework.Utils.ToastUtil;
import com.framework.Utils.Utils;
import com.framework.Utils.Y;
import com.framework2.base.BaseOnClickListener;
import com.framework2.base.BiuooBaseActivity;
import com.framework2.customview.TitleView;
import com.framework2.utils.BiuooCollectionUtil;
import com.framework2.utils.BiuooCustomProgressDialogUtils;
import com.framework2.utils.PicToastUtil;
import com.asiainfo.heshanghui.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商品类型
 */
public class BiuooGoodsTypeActivity extends BiuooBaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private boolean requestChooseTypeSuccess;
    private UniversalAdapter<GoodsTypeInfo> adapter;
    private List<GoodsTypeInfo> list_ = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setTranslucentStatus(this, true);
        Utils.setStatusBarTintDrawable(this, R.mipmap.biuoo_bg_top);
        setContentView(R.layout.biuoo_activity_goods_type);
        ButterKnife.bind(this);
        initView();
        initDataRecyclerView();
        BiuooCustomProgressDialogUtils.showProcessDialog(this, "正在请求");
        requestList(getIntent().getStringExtra(ConstantsME._interface), getIntent().getIntExtra(ConstantsME.position, 0),PreferencesHelper.getInstance().getStringData(ConstantsME.currentShopId), getIntent().getStringExtra(ConstantsME.classId), getIntent().getStringExtra(ConstantsME.brandId), getIntent().getStringExtra(ConstantsME.modelId));//InterfaceConfig.goodsType
    }

    private void initView() {
        titleView.setTitle(TextUtils.isEmpty(getIntent().getStringExtra(ConstantsME.title)) ? "商品出库" : getIntent().getStringExtra(ConstantsME.title)).setRightVisible(false);
    }

    @SuppressWarnings("unchecked")
    private void initDataRecyclerView() {
        //第一种，简单的颜色，高度,水平分割线
        HorizontalDividerItemDecoration2 horizontalDividerItemDecoration = new HorizontalDividerItemDecoration2(BiuooGoodsTypeActivity.this);
        horizontalDividerItemDecoration.setColor(this, R.color.gray);
        horizontalDividerItemDecoration.setDividerHeightPx(1);
        recyclerView.addItemDecoration(horizontalDividerItemDecoration);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = initAdapter(adapter);
        recyclerView.setAdapter(adapter);
    }

    @SuppressWarnings("unchecked")
    private UniversalAdapter initAdapter(UniversalAdapter adapter) {
        if (adapter == null) {
            adapter = new UniversalAdapter<GoodsTypeInfo>(BiuooGoodsTypeActivity.this, R.layout.biuoo_listitem_goods_type, list_) {
                @Override
                protected void getItemView(UniversalViewHolder viewHolder, final GoodsTypeInfo info, int position) {
                    viewHolder.setText(R.id.tvTitle, info.getTitle());
                    viewHolder.getView(R.id.tvTitle).setOnClickListener(new BaseOnClickListener() {
                        @Override
                        protected void onBaseClick(View v) {
                            if (TextUtils.equals("商品类型", getIntent().getStringExtra(ConstantsME.title))) {
                                EventBus.getDefault().post(info, EventBusTag.GOODS_TYPE);
                            } else if (TextUtils.equals("品牌", getIntent().getStringExtra(ConstantsME.title))) {
                                EventBus.getDefault().post(info, EventBusTag.BRAND);
                            } else if (TextUtils.equals("类型", getIntent().getStringExtra(ConstantsME.title))) {
                                EventBus.getDefault().post(info, EventBusTag.TYPE);
                            }
                            finishActivity();
                        }
                    });

                }
            };
        }
        return adapter;
    }

    /**
     * 请求列表
     */
    private void requestList(@NonNull String _interface, int position, @NonNull final String shopId, @NonNull final String classId, @NonNull final String brandId, @NonNull final String modelId) {
        requestChooseTypeSuccess = false;
        HttpUtil.getInstance().requestGoodsTypeList(this, position, _interface,shopId,classId,brandId,modelId, new HttpUtil.OnRequestListResult<GoodsTypeInfo>() {
            @Override
            public void onSuccess(List<GoodsTypeInfo> list, String... msg) {
                requestChooseTypeSuccess = true;
                if (!BiuooCollectionUtil.getInstance().isEmpty(list)) {
                    list_.clear();
                    list_.addAll(list);
                    adapter.notifyDataSetChanged();
                } else {
                    PicToastUtil.showPicToast(BiuooGoodsTypeActivity.this, "列表为空");
                }
            }

            @Override
            public void onFail(int code, String msg) {
                requestChooseTypeSuccess = false;
                PicToastUtil.showPicToast(BiuooGoodsTypeActivity.this, TextUtils.isEmpty(msg) ? "请求异常" : msg);
            }
        });
    }
}
